<?php

class ClassCourseTrainnerRateController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'ClassCourseTrainnerRate'),
        ));
    }

    public function actiontrainerRate() {
        $model = new ClassCourseTrainnerRate;
        if ($_POST['userId']) {
            $model->setAttribute('status', $_POST['status']);
            $model->setAttribute('from_user_id', getLoginUserId());
            $model->setAttribute('create_time', SkillConfig::CreateDateTime());
            $userType = ($_POST['userType'] != 'backend') ? 'to_front_user_id' : 'to_back_user_id';
            $model->setAttribute($userType, $_POST['userId']);
            $reviewTrainer = ClassCourseTrainnerRate::model()->findByAttributes(array($userType => $_POST['userId'], 'from_user_id' => getLoginUserId()));

            if (!empty($reviewTrainer)) {
                $reviewTrainer->setAttribute('status', $_POST['status']);
                $reviewTrainer->setAttribute('from_user_id', getLoginUserId());
                $reviewTrainer->setAttribute('create_time', SkillConfig::CreateDateTime());
                $reviewTrainer->setAttribute($userType, $_POST['userId']);
                $reviewTrainer->save();
                echo $reviewTrainer->status;
            } else {
                $model->save();
                echo 'success';
            }
        }
    }

    public function actionclassCourseRate() {
        $model = new ClassCourseTrainnerRate;
        $classCoursetype = (isset($_POST['classId'])) ? 'classes_id' : 'courses_id';
        $classCourseVal = (isset($_POST['classId'])) ? $_POST['classId'] : $_POST['courseId'];
        $rateClassCourse = ClassCourseTrainnerRate::model()->findByAttributes(array($classCoursetype => $classCourseVal, 'from_user_id' => getLoginUserId()));
        $model->setAttribute('status', $_POST['status']);
        $model->setAttribute('from_user_id', getLoginUserId());
        $model->setAttribute('create_time', SkillConfig::CreateDateTime());
        $model->setAttribute($classCoursetype, $classCourseVal);
        if (!empty($rateClassCourse)) {
            $rateClassCourse->setAttribute('status', $_POST['status']);
            $rateClassCourse->setAttribute('from_user_id', getLoginUserId());
            $rateClassCourse->setAttribute('create_time', SkillConfig::CreateDateTime());
            $rateClassCourse->setAttribute($classCoursetype, $classCourseVal);
            $rateClassCourse->save();
            echo $rateClassCourse->status;
        } else {
            $model->save();
            echo 'success';
        }
    }

}